package com.wanmait.recycle.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

	//写cookie
	public static void addCookie(String name,String value,String path,int maxAge,HttpServletResponse response) {
		
		//创建cookie
		Cookie cookie = new Cookie(name,value);
		//设置生命周期
		cookie.setMaxAge(maxAge);
		//设置作用路径
		cookie.setPath(path);
		//添加cookie
		response.addCookie(cookie);
	} 
	//读cookie值
	public static String getValue(String name,HttpServletRequest request) {
		
		String value = null;
		Cookie[] cookies = request.getCookies();
		if(cookies!=null) {
			for (Cookie cookie : cookies) {
				if(cookie.getName().equals(name)) {
					value = cookie.getValue();
					break;
				}
			}
		}
		return value;
	}
	
}
